package com.alliky.classify.ui.fragment

import android.os.Bundle
import android.view.View
import androidx.fragment.app.activityViewModels
import androidx.lifecycle.lifecycleScope
import androidx.paging.ExperimentalPagingApi
import androidx.paging.LoadState
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alliky.classify.R
import com.alliky.classify.ui.adapter.ClassifyHotsAdapter
import com.alliky.classify.ui.adapter.ClassifyTitleAdapter
import com.alliky.classify.databinding.FragmentClassifyBinding
import com.alliky.classify.ui.viewmodel.ClassifyViewModel
import com.alliky.common.router.RoutePath
import com.alliky.core.base.BaseFragment
import com.alliky.core.callback.OnItemClickListener
import com.rrc.common.entity.CategoryVO
import com.rrc.common.entity.GoodsVO
import com.alliky.core.net.IStateObserver
import com.alliky.core.utils.RxToast
import kotlinx.android.synthetic.main.fragment_classify.*
import kotlinx.coroutines.flow.collectLatest

@ExperimentalPagingApi
@Route(path = RoutePath.Classify.FRAGMENT_CLASSIFY)
class ClassifyFragment : BaseFragment<FragmentClassifyBinding>() {

    private val mViewModel: ClassifyViewModel by activityViewModels()

    private var mTitleAdapter = ClassifyTitleAdapter(ArrayList())

    var mClassifyHotsAdapter: ClassifyHotsAdapter = ClassifyHotsAdapter()

    override fun getLayoutId(): Int {
        return R.layout.fragment_classify
    }

    override fun initData(savedInstanceState: Bundle?) {
        mBinding.recyclerViewTitle.apply {
            layoutManager = LinearLayoutManager(requireContext())
            adapter = mTitleAdapter
        }
        mViewModel.getCategoryData(HashMap())

        mTitleAdapter.setOnItemChildClickListener { adapter, view, position ->
            var list = adapter.data as List<CategoryVO>

            for (item in list) {
                item.selected = false
            }
            list[position].selected = true
            adapter.notifyDataSetChanged()
        }

        mBinding.recyclerViewHot.apply {
            layoutManager = GridLayoutManager(requireContext(), 2)
            adapter = mClassifyHotsAdapter
        }

        mClassifyHotsAdapter.setOnItemClickListener(object : OnItemClickListener<GoodsVO> {
            override fun onItemClick(view: View?, item: GoodsVO?, position: Int) {
                RxToast.normal(item?.goods_name)
            }
        })

        mBinding.refreshLayout.setOnRefreshListener {
            refreshLayout.postDelayed({
                mClassifyHotsAdapter.refresh()
//                mTitleAdapter.refresh()
            }, 500)
        }

        lifecycleScope.launchWhenCreated {
            mClassifyHotsAdapter.loadStateFlow.collectLatest {
                mBinding.refreshLayout.isRefreshing = it.refresh is LoadState.Loading
            }
//            mTitleAdapter.loadStateFlow.collectLatest {
//                mBinding.refreshLayout.isRefreshing = it.refresh is LoadState.Loading
//            }
        }

        lifecycleScope.launchWhenCreated {
            mViewModel.hotGoodsPagingFlow().collectLatest { data ->
                mClassifyHotsAdapter!!.submitData(data)
            }
        }

//        lifecycleScope.launchWhenCreated {
//            mViewModel.categoryDataFlow().collectLatest { data ->
//                mTitleAdapter!!.submitData(data)
//            }
//        }

        mViewModel.getCategoryData(HashMap())
    }

    override fun initObservable() {
        mViewModel.categoryLiveData.observe(this, object : IStateObserver<List<CategoryVO>>(null) {
            override fun onDataChange(data: List<CategoryVO>?) {
                super.onDataChange(data)
//                var list = mTitleAdapter.data as List<CategoryVO>
//
//                var id: Int = 0
//
//                for (item in list) {
//                    if (item.selected) {
//                        id = item.id
//                    }
//                }

//                if (id == 0) {
                if (data!!.isNotEmpty()) {
                    data[0].selected = true
                }
//                }

                mTitleAdapter.setNewData(data)
            }
        })
    }
}